package com.appleyk.core.common;

import com.appleyk.core.dict.ResultCode;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import java.util.Date;

/**
 * <p>响应结果包装类</p>
 *
 * @author appleyk
 * @version V.1.0.0
 * @blob https://blog.csdn.net/appleyk
 * @date created on 上午 9:00 2019-4-27
 */
public class SnResult {

    /**
     * 响应结果状态码
     */
    private  Integer status;

    /**
     * 响应结果消息
     */
    private  String message;

    /**
     * 响应结果对应的（包含）的数据，空的话不反序列话
     */
    @JsonInclude(value = Include.NON_NULL)
    private  Object data;

    /**
     * 响应时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private  Date timeStamp = new Date();

    public SnResult() {
        this.status = null;
        this.message = null;
        this.data = null;
    }

    public SnResult(Integer status, String message) {
        this.status = status;
        this.message = message;
        this.data = null;
    }

    public SnResult(Integer status , String message, Object data){
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public SnResult(ResultCode resultCode, String message){
         this.status = resultCode.getCode();
         this.message = message;
    }

    /**
     * 默认成功返回的实例
     * @param data
     */
    private SnResult(Object data){
        this.status = ResultCode.OK.getCode();
        this.message = ResultCode.OK.getName();
        this.data = data;
    }

    private SnResult(ResultCode code){
        this.status = code.getCode();
        this.message = code.getName();
        this.data = null;
    }

    public Integer getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public Object getData() {
        return data;
    }

    public Date getTimeStamp() {
        return timeStamp;
    }

    public static SnResult ok(Object data){
        return new SnResult(data);
    }

    public static SnResult fail(ResultCode code){
       return new SnResult(code);
    }

    public static SnResult fail(String message){
        return new SnResult(500,"失败",message);
    }

}
